package com.example.messages.test;
import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;


public class Send {

    private final static String QUEUE_NAME = "MessageService";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("106.3.97.187");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("/HealthBox");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            channel.queueDeclare(QUEUE_NAME, true, false, false, null);
            //String message = "18978930036";
//            channel.basicPublish("", QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
//            System.out.println(" [x] Sent '" + message + "'");

            Student student = new Student("嘿嘿",1);

            String message = JSON.toJSONString(student);
//            ByteArrayOutputStream ba=new ByteArrayOutputStream();
//            ObjectOutputStream oos=new ObjectOutputStream(ba);
//            oos.writeObject(student);
           // byte[] studentBytes=ba.toByteArray();
            channel.basicPublish("", QUEUE_NAME, null, JSON.toJSONBytes(student));
            System.out.println("已发送对象Student字节流");
        }
    }
    public static String hex(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (byte aByte : bytes) {
            result.append(String.format("%02x", aByte));
        }
        return result.toString();
    }
}